package com.example.rabbitmqextensions.listener;

import com.example.rabbitmqextensions.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Component
public class QosListener {
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void Listener1(Message message, Channel channel) throws InterruptedException, IOException {
        long deliverTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("Listener1 %tc 接收到消息: %s, deliverTag: %d\n", new Date(), new String(message.getBody()), deliverTag);
        Thread.sleep(1000);
        channel.basicAck(deliverTag, false);
    }

    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void Listener2(Message message, Channel channel) throws IOException, InterruptedException {
        long deliverTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("Listener2 %tc 接收到消息: %s, deliverTag: %d\n", new Date(), new String(message.getBody()), deliverTag);
        Thread.sleep(2000);
        channel.basicAck(deliverTag, false);
    }
}
